package com.android.thought.badboys.Alarm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.android.thought.badboys.SharedPreferenceUtil;
import com.android.thought.badboys.Info.PhoneInfo;

public class AlarmServices {
	
	final public String KEY_JSON_NUMBER = "NUMBER";
	final public String KEY_JSON_ID = "ID";
	final public String KEY_JSON_TIME = "TIME";
	final public String KEY_JSON_SEND = "SENDING";

	Context mContext;
	AlarmManager mAlarmManager;

	public AlarmServices(Context context) {
		this.mContext = context;
		mAlarmManager = (AlarmManager) this.mContext.getSystemService(Context.ALARM_SERVICE);
	}
	
	
	public void onSingleRegister(int ids, long time) { 
		Intent intent = new Intent(mContext, AlarmReceive.class);
		intent.putExtra("DATA", ids);
		PendingIntent sender = PendingIntent.getBroadcast(mContext, ids, intent, 0);
		
		// 알람 예약 //
		mAlarmManager.set(AlarmManager.RTC, time, sender);// 이건 한번 알람
	}

	public void onRegister(ArrayList<PhoneInfo> listInfo) {
		this.onDelete();
		
		HashSet<String> hashSet = new HashSet<String>();
		hashSet = (HashSet<String>) SharedPreferenceUtil.getValue(SharedPreferenceUtil.PREF_INTRO_MESSAGE_SET_INFO,  hashSet);
		
		final Calendar calendar = Calendar.getInstance();
		long currentTime = System.currentTimeMillis();
		calendar.setTimeInMillis(currentTime);

		calendar.set(Calendar.HOUR_OF_DAY, 12);
		calendar.set(Calendar.MINUTE, 00);
		calendar.set(Calendar.SECOND, 00);
				
		long time12Clock = calendar.getTimeInMillis();
		final Random random = new Random(1000);

		for (PhoneInfo info : listInfo) {
			String phonenumber = info.getPhoneNumber().replace("-", "").replace(" ", "");
			int ids = Integer.parseInt(phonenumber);
			
			long sendTime  = -1;
			if ( currentTime < time12Clock ) { 
				// 12시 이전이라면...   12시 몇분으로 랜덤 발송 설정
				sendTime = time12Clock + (random.nextInt(60) * 60 * 1000);
			} else if ( currentTime >= time12Clock && currentTime <= (time12Clock + (59 * 60 * 1000))) { 
				// 12 시 ~ 12시 50분에 설정하면.
				
				sendTime = currentTime + (30 * 1000) ; // 30초 있다가 메시지 발생.
			} else {
				// 이미 지났으면.... 다음날로 설정.
				sendTime = time12Clock + ( 12 * 60 * 60 * 1000 * 2 ) + (random.nextInt(60) * 60 * 1000);
			}

			if ( sendTime != -1 ) {
				Intent intent = new Intent(mContext, AlarmReceive.class);
				intent.putExtra("DATA", ids);
				PendingIntent sender = PendingIntent.getBroadcast(mContext, ids, intent, 0);

				// 알람 예약 //
				mAlarmManager.set(AlarmManager.RTC, sendTime, sender);// 이건 한번 알람
			}
			

		}
	}

	public void onDelete() {
		HashSet<String> hashSet = new HashSet<String>();
		SharedPreferenceUtil.getInstance(this.mContext);

		hashSet = (HashSet<String>) SharedPreferenceUtil.getValue(SharedPreferenceUtil.PREF_INTRO_MESSAGE_SET_INFO,
				hashSet);

		Intent intent = new Intent(mContext, AlarmReceive.class);

		for (String value : hashSet) {
			try {
				
				int ids = Integer.parseInt(value);

				intent.putExtra("DATA", ids);
				PendingIntent sender = PendingIntent.getBroadcast(mContext, ids, intent, 0);
				mAlarmManager.cancel(sender);
			} catch (Exception e) {
			}
		}
		
		hashSet.clear();
		SharedPreferenceUtil.put(SharedPreferenceUtil.PREF_INTRO_MESSAGE_SET_INFO, hashSet);
	}
}
